package com.wei.stavix.ui.module.account

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavType
import androidx.navigation.Navigator
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import com.wei.stavix.R
import com.wei.stavix.ui.theme.ColorGray
import com.wei.stavix.ui.theme.PaddingPage
import com.wei.stavix.ui.widget.LoginInput
import com.wei.stavix.ui.widget.PhoneView
import com.wei.stavix.ui.widget.clickTime

@Composable
fun AccountHost(viewModel: AccountViewModel) {

    //    var backHandlingEnabled by remember { mutableStateOf(true) }
//    BackHandler(backHandlingEnabled) {
//        // Handle back press
//        navController.popBackStack()
//    }
    val navController = rememberNavController()
    NavHost(navController = navController, startDestination = "Account") {
        composable("Account") { LoginPageView(viewModel, navController) }
        composable("Account?userId={userId}", arguments = listOf(navArgument("userId") { defaultValue = "me" })) {
                backStackEntry -> RegisterPageView(viewModel, navController, backStackEntry.arguments?.getString("userId"))
        }
    }
//    NavHost(navController = navController, startDestination = "profile/{userId}") {
////        composable(
////            "profile/{userId}",
////            arguments = listOf(navArgument("userId") { type = NavType.StringType })
////        ) {}
////        composable("profile/{userId}") { backStackEntry ->
////            LoginPageView(navController, backStackEntry.arguments?.getString("userId"))
////            navController.navigate("profile/user1234")
////        }
//        composable(
//            "profile?userId={userId}",
//            arguments = listOf(navArgument("userId") { defaultValue = "me" })
//        ) { backStackEntry ->
////            Profile(navController, backStackEntry.arguments?.getString("userId"))
//        }
//    }
}

@Composable
fun LoginPageView(viewModel: AccountViewModel, navController: NavHostController) {
//    var viewModel2: AccountViewModel = viewModel()
    Column(
        horizontalAlignment = Alignment.CenterHorizontally,
        modifier = Modifier
            .background(color = Color.White)
            .padding(PaddingPage)
            .background(color = Color.White)
    ) {
        var txtUser by remember { mutableStateOf("请输入帐号") }
        var txtPwd by remember { mutableStateOf("") }
        Spacer(modifier = Modifier.height(40.dp))

        Image(
            painter = painterResource(id = R.drawable.login_logo),
            contentDescription = null,
            modifier = Modifier.clickable { viewModel.onTopBackClick(1) }
        )

        Spacer(modifier = Modifier.height(27.dp))

        Row(modifier = Modifier.fillMaxWidth()) {
            PhoneView()
        }

        Spacer(modifier = Modifier.height(27.dp))
        LoginInput(1)

        Spacer(modifier = Modifier.height(27.dp))

        LoginInput(2)

        Spacer(modifier = Modifier.height(70.dp))

        Button(
            onClick = { viewModel.clickLogin() },
            shape = RoundedCornerShape(8.dp),
            modifier = Modifier.fillMaxWidth()
        ) {
            Text(
                text = "登 录",
                textAlign = TextAlign.Center,
                fontSize = 16.sp,
                color = Color.White,
            )
        }

        Spacer(modifier = Modifier.weight(1f))

        Row(
            horizontalArrangement = Arrangement.Center,
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(
                text = "注册",
                fontSize = 14.sp,
                color = ColorGray,
                textAlign = TextAlign.End,
                modifier = Modifier
                    .weight(1f)
                    .clickTime {
                        navController.navigate("Account?userId=abc")
//                        navController.navigate("RegisterPageView")
                        viewModel.launch2Register()
                    }
            )

            Text(
                text = "｜",
                fontSize = 14.sp,
                color = ColorGray,
                modifier = Modifier.padding(start = 16.dp, end = 16.dp)
            )
            Text(
                text = "忘记密码",
                fontSize = 14.sp,
                color = ColorGray,
                textAlign = TextAlign.Start,
                modifier = Modifier
                    .weight(1f)
                    .clickable { viewModel.launch2ForgotPwd() }
            )
        }
    }
}